'
'   XC Engine. A simple engine to drive the Internet Explorer web browser
'   Copyright (C) 2009 Stanislav Hordiyenko
'
'   This program is free software; you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation; either version 3 of the License, or
'   (at your option) any later version.
'
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.
'
'   You should have received a copy of the GNU General Public License
'   along with this program; if not, see http://www.gnu.org/licenses
'   or write to the Free Software Foundation,Inc., 51 Franklin Street,
'   Fifth Floor, Boston, MA 02110-1301 USA
'

Class Listbox
	Private objHtmlListbox
	Private strHtmlListboxUniqueId
	
	Public Sub Class_Initialize()
		Set objHtmlListbox = Nothing
	End Sub
	
	Public Sub Class_Terminate()
		Set objHtmlListbox = Nothing
	End Sub
	
	Private Function IsListbox(ByVal objHtmlElement)
		IsListbox = False
		
		If UCase(objHtmlElement.TagName) = "SELECT" Then
			IsListbox = True
		End If
	End Function

	Public Sub WrapElement(ByVal objHtmlElement)
		If Not (objHtmlElement Is Nothing) Then
			If IsListbox(objHtmlElement) Then
				strHtmlListboxUniqueId = objHtmlElement.UniqueId
			
				Set objHtmlListbox = objHtmlElement
				
				Exit Sub
			End If
		End If

		Err.Raise 10001, "Listbox->WrapElement", "The Html Element couldn't be wrapped"
	End Sub
	
	Public Function GetProperty(ByVal strPropertyName)
		If Exists() Then
			If Internal_GetProperty(objHtmlListbox, strPropertyName, varPropertyName) Then
				GetProperty = varPropertyName
			Else
				Err.Raise 10003, "Listbox->GetProperty", "The List Box doesn't support this property"
			End If
		Else
			Err.Raise 10002, "Listbox->GetProperty", "The List Box doesn't exist"
		End If
	End Function
	
	Public Function Element()
		Set Element = Nothing
		
		If Not Exists() Then
			Err.Raise 10002,  "Listbox->Element", "The List Box doesn't exist"
			
			Exit Function
		End If
		
		Set Element = objHtmlListbox
	End Function
	
	Public Function Exists()
		On Error Resume Next
		
		Exists = False
		
		If Not IsListbox(objHtmlListbox) Then Exit Function
		
		strHtmlElementTagName = objHtmlListbox.TagName
		
		If Err.Number = 0 Then
			Exists = True
			
			Exit Function
		End If
		
		Err.Clear
		
		On Error Goto 0
	End Function	
	
	Public Sub FireEvent(ByVal strEventName)
		If Exists() Then
			objHtmlListbox.FireEvent strEventName
		Else
			Err.Raise 10002, "Listbox->FireEvent", "The List Box doesn't exist"
		End If
	End Sub
	
	Public Sub ExtendSelectItem(ByVal varValue)
		If Not Exists() Then
			Err.Raise 10002, "Listbox->ExtendSelectItem", "The List Box doesn't exist"
			
			Exit Sub
		End If
		
		If GetProperty("disabled") Then
			Err.Raise 10004, "Listbox->ExtendSelectItem", "The List Box is disabled"
			
			Exit Sub
		End If
		
		If GetProperty("multiple") = True Then
			Call SelectMethod(varValue, "extend select")
		Else
			Call SelectMethod(varValue, "select")
		End If
	End Sub

	Public Sub SelectItem(ByVal varValue)
		If Not Exists() Then
			Err.Raise 10002, "Listbox->SelectItem", "The List Box doesn't exist"
			
			Exit Sub
		End If
		
		If GetProperty("disabled") Then
			Err.Raise 10004, "Listbox->SelectItem", "The List Box is disabled"
			
			Exit Sub
		End If
	
		Call SelectMethod(varValue, "select")
	End Sub
	
	Public Sub DeselectItem(ByVal varValue)
		If Not Exists() Then
			Err.Raise 10002, "Listbox->DeselectItem", "The List Box doesn't exist"
			
			Exit Sub
		End If
		
		If GetProperty("disabled") Then
			Err.Raise 10004, "Listbox->DeselectItem", "The List Box is disabled"
			
			Exit Sub
		End If
	
		Call SelectMethod(varValue, "deselect")
	End Sub
	
	Private Function SelectMethod(ByVal varValue, ByVal strMode)
		Dim strModeSelect
	
		strMode = LCase(strMode)
	
		If Left(varValue, 1) = "#" And Left(varValue, 2) <> "##" Then
			intIndexValue = Right(varValue, Len(varValue) - 1)
			
			If Not IsNumeric(intIndexValue) Then
				SelectMethod = False
				
				Exit Function
			End If
			
			intIndexValue = CInt(intIndexValue)
			strModeSelect = "by index"
			
			If intIndexValue < 0 Or intIndexValue > objHtmlListbox.Options.Length - 1 Then
				SelectMethod = False
				
				Exit Function
			End If
		Else
			varValue = Trim(varValue)
			strModeSelect = "by text"
		End If
		
		If (strMode = "deselect" Or strMode = "extend select") And strModeSelect = "by index" Then
			If strMode = "deselect" Then
				objHtmlListbox.Options(intIndexValue).Selected = False
				SelectMethod = True
			Else
				objHtmlListbox.Options(intIndexValue).Selected = True
				SelectMethod = True
			End If
		Else
			Dim intItemCount
			Dim blnFind
			
			blnFind = False
			intItemCount = objHtmlListbox.Options.Length
		
			For i = 0 To intItemCount - 1
				If strModeSelect = "by text" Then
					sItemValue = Replace(Trim(objHtmlListbox.Options(i).Text),Chr(160),"")
					
					If sItemValue = varValue Then
						If strMode = "deselect" Then
							objHtmlListbox.Options(i).Selected = False
							blnFind = True
							
							Exit For
						ElseIf strMode = "extend select" Then
							objHtmlListbox.Options(i).Selected = True
							blnFind = True
							
							Exit For
						Else
							objHtmlListbox.Options(i).Selected = True
						End If
					Else
						If strMode = "select" Then
							objHtmlListbox.Options(i).Selected = False
						End If
					End If
				Else
					objHtmlListbox.Options(i).Selected = False
				End If
			Next
			
			If strModeSelect = "by index" And strMode = "select" Then
				objHtmlListbox.Options(intIndexValue).Selected = True
				blnFind = True
			End If
		End If
		
		If blnFind = True Then			
			SelectMethod = True
		Else
			SelectMethod = False
		End If
	End Function
End Class